//
//  JKViewShopsProducts.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/29.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKViewShopsProducts.h"

// model
#import "ByModelShopCate.h"
@implementation JKViewShopsProducts


- (void)setProducts:(NSArray *)products {
    
    _products = products;
    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIView *last = nil;
    for (int i = 0; i < _products.count; i ++) {
        
        UIControl *view = [self basalViewWithTag:10 + i];
        [view addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:view];
        
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if (last) {
                // 每一行的第一个，last是上一行的右侧那个
                if (i % 4 == 0) {
                    make.left.equalTo(@0);
                    make.width.equalTo(last);
                    make.top.equalTo(last.mas_bottom).offset(10);
                }
                // last是本行的前一个
                else {
                    make.left.equalTo(last.mas_right).offset(10);
                    make.width.equalTo(last);
                    make.top.equalTo(last);
                }
            } else {
                make.left.equalTo(@0);
                make.width.equalTo(@((SCREEN_WIDTH - 10 * 5) / 4));
                make.top.equalTo(@0);
            }
            
            // 设置cell高度
            if (i == _products.count - 1) {
                make.bottom.equalTo(@-10);
            }
        }];
        
        last = view;
        
        
        
        
    }
}

- (UIControl *)basalViewWithTag:(NSInteger)tag {
    
    UIControl *basalView = [UIControl new];
    basalView.tag = tag;
    
    ByModelShopCate *model = _products[tag - 10];
    
    // 商家图片
    UIImageView *iconView = [UIImageView new];
    [iconView setImageWithURL:[NSURL URLWithString:model.iconUrl] placeholderImage:DEFAULT_IMAGE completed:nil];
    iconView.layer.cornerRadius = 2;
    iconView.layer.masksToBounds = true;
    [basalView addSubview:iconView];
    
    // 名字
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = model.name;
    nameLabel.font = FONT(14);
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.textColor = [UIColor blackColor];
    [basalView addSubview:nameLabel];

    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@0);
        make.width.equalTo(basalView);
        make.height.equalTo(iconView.mas_width);
    }];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@5);
        make.right.equalTo(@-5);
        make.top.equalTo(iconView.mas_bottom).offset(0);
        make.height.equalTo(@25);
        
        make.bottom.equalTo(@0);
    }];

    
    return basalView;
}

- (void)tapAction:(UIControl *)view {
    
    if (self.selectBlock) {
        self.selectBlock(view.tag - 10);
    }
}


@end
